package handler

import (
	"context"
	"net/http"

	"github.com/gin-gonic/gin"
	"github.com/go-micro/plugins/v4/registry/consul"
	"go-micro.dev/v4"
	"go-micro.dev/v4/client"
	"go-micro.dev/v4/registry"

	"pan/common"
	"pan/config"

	// downloadProto "pan/service/download/proto"
	// uploadProto "pan/service/upload/proto"
	userProto "pan/service/user/proto"
	"pan/util"
)

var (
	userCli userProto.UserService
	// uploadCli   uploadProto.UploadService
	// downloadCli downloadProto.DownloadService
)

func init() {
	consulRegistry := consul.NewRegistry(registry.Addrs("127.0.0.1:8500"))
	service := micro.NewService(
		micro.Client(client.NewClient()),
		micro.Registry(consulRegistry),
	)
	service.Init()
	cli := service.Client()
	// 初始化一个user服务的客户端
	userCli = userProto.NewUserService("user", cli)
	// 初始化一个upload服务的客户端
	// uploadCli = uploadProto.NewUploadService("upload", cli)
	// 初始化一个download服务的客户端
	// downloadCli = downloadProto.NewDownloadService("download", cli)
}

// SignupHandler : 响应注册页面
// func SignupHandler(c *gin.Context) {
// 	c.Redirect(http.StatusFound, "/static/view/signup.html")
// }

// DoSignupHandler : 处理注册post请求
func DoSignupHandler(c *gin.Context) {
	username := c.Request.FormValue("username")
	passwd := c.Request.FormValue("password")
	rpcResp, err := userCli.Signup(context.TODO(), &userProto.ReqSignup{
		Username: username,
		Password: passwd,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": rpcResp.Code,
			"msg":  rpcResp.Message,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": rpcResp.Code,
		"msg":  rpcResp.Message,
	})
}

// DoSigninHandler : 处理登录post请求
func DoSigninHandler(c *gin.Context) {
	username := c.Request.FormValue("username")
	password := c.Request.FormValue("password")
	// 调用user服务
	rpcResp, err := userCli.Signin(context.TODO(), &userProto.ReqSignin{
		Username: username,
		Password: password,
	})
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"msg":  rpcResp.Message,
			"code": rpcResp.Code,
		})
		return
	}
	if rpcResp.Code != common.StatusOK {
		c.JSON(http.StatusOK, gin.H{
			"msg":  rpcResp.Message,
			"code": rpcResp.Code,
		})
		return
	}
	// 登录成功，返回用户信息
	cliResp := util.RespMsg{
		Code: rpcResp.Code,
		Msg:  rpcResp.Message,
		Data: struct {
			Location      string
			Username      string
			Token         string
			UploadEntry   string
			DownloadEntry string
		}{
			Username:      username,
			Token:         rpcResp.Token,
			UploadEntry:   config.UploadLBHost,
			DownloadEntry: config.DownloadLBHost,
		},
	}
	c.Data(http.StatusOK, "application/json", cliResp.JSONBytes())
}

// UserInfoHandler: 查询用户信息
func UserInfoHandler(c *gin.Context) {
	username := c.Request.FormValue("username")
	rpcResp, err := userCli.UserInfo(context.TODO(), &userProto.ReqUserInfo{
		Username: username,
	})
	if err != nil {
		c.Status(http.StatusInternalServerError)
		return
	}
	cliResp := util.RespMsg{
		Code: rpcResp.Code,
		Msg:  rpcResp.Message,
		Data: gin.H{
			"Username":   username,
			"SignUpAt":   rpcResp.SignupAt,
			"LastActive": rpcResp.LastActiveAt,
		},
	}
	c.Data(http.StatusOK, "application/json", cliResp.JSONBytes())
}
